/**
* @Title: integrityMedical_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:36:25
* @version V1.0
*/
;
(function(){
	'use strict';	
	var app = angular.module('fwa.mainApp');
	app.controller('IntegrityMedicalApproveCtrl',['$scope','BaseURL','$stateParams','CommunityService','IntegrityMedicalService','Modal','$modal',
	                                             function($scope,baseUrl,$stateParams,CommunityService,IntegrityMedicalService,Modal,$modal){
		$scope.form = {};
		var currentPage = 1;
		$scope.form.date = {};
		$scope.integrityMedicalService = IntegrityMedicalService;
		IntegrityMedicalService.initMedicalOrgInfoList();
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="queryDetail(data.doctorCode,data.name,data.certificateNo)">查看详情</a>'+
			'</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
		var cellTemplateSp = 
			'<td>'+
				'<a  ng-click="ApproveSp(data.taskNo,data.insuranceName,data.doctorCode,data.updatedBy)">{{data.tranName}}</a>'+
			'</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
		$scope.columns = [
		      		    {label:'姓名',map:'insuranceName', width:'25%'},
		      		    {label:'证件号码',map:'certificateNo', width:'30%'},
		      		    {label:'违规记分值',map:'pointTotal', width:'15%',cellClass:'text-center'},
		      		    {label:'违规记分明细',map:'status', width:'15%',cellClass:'text-center', cellTemplate:cellTemplate},
		      		    {label:'操作',map:'status', width:'15%',cellClass:'text-center', cellTemplate:cellTemplateSp}
		      		 ];
		var params = function (){
			var doctorCode=null;
			if($scope.form.doctorCode&&!angular.isUndefined($scope.form.doctorCode)){
				doctorCode = $scope.form.doctorCode;
			}
			var orgCode=null;
			if($scope.form.orgCode&&!angular.isUndefined($scope.form.orgCode)){
				orgCode = $scope.form.orgCode;
			}
			var start = 0,
			 	limit = 20;
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					startDate:$scope.form.date.start,
					endDate:$scope.form.date.end,
					doctorCode:doctorCode,
					orgCode:orgCode,
					actCurrent:'N2015003',
					start:start,
					limit:limit
			}
			return params;
		}
		function queryIntegrityMedical() {
			var par = params();
			if(!par.startDate||!par.endDate){
				Modal.alert("案发时间为必录！");
				return null;
			}
			//查询医护人员审批列表
			IntegrityMedicalService.getMedicalApproveList(params()).
			success(function(res){
				$scope.tableData = res.result;
				$scope.totalCount = res.totals.total;
			});
		}
		 $scope.onSubmit = function(){
         	currentPage = 1;
         	queryIntegrityMedical();
		}
		
		$scope.$on('pageTool.skipPage',function(e,v){
			var id=e.targetScope.id;
		     if(id=='parentPageTool'){
				currentPage = v;
				queryIntegrityMedical();	 
		     }
		});
		
		$scope.$watch('form.doctorCode', function(v){
			if(v){
				$scope.onSubmit();
			}
		});
		//弹框显示审批页面
		$scope.ApproveSp=function(taskNo,doctorName,doctorCode,updatedBy){
			IntegrityMedicalService.taskNo = taskNo;
			IntegrityMedicalService.doctorName = doctorName;
			IntegrityMedicalService.doctorCode = doctorCode;
			IntegrityMedicalService.startDate = $scope.form.date.start;
			IntegrityMedicalService.endDate = $scope.form.date.end;
			IntegrityMedicalService.saveStatus = '3';//审批
			IntegrityMedicalService.updatedBy = updatedBy;//最近操作人
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/integrityApproveDialog_view.html',
			      backdrop:'static',
				  size:'sm',
//				  scope:scope,
				  controller: 'IntegrityApproveCtrl'
			    });
			//接收返回数据
			 modalInstance.result.then(function(isTrue) {//true:审核成功
				 if (isTrue) {
					 $scope.onSubmit();
					}
	 	    });
		}
		
		//查看详情
		$scope.queryDetail = function (doctorCode,doctorName,certificateNo){
			IntegrityMedicalService.doctorCode = doctorCode;
			IntegrityMedicalService.doctorName = doctorName;
			IntegrityMedicalService.startDate = $scope.form.date.start;
			IntegrityMedicalService.endDate = $scope.form.date.end;
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/integrityPointDescDialog_view.html',
			      backdrop:'static',
				  size:'sm',
//				  scope:scope,
				  controller: 'InsuredQueryDialogCtrl'
			    });
		}
		//批量信息
        function getBatchArtificialScoreData(){
			var batchArtificialScoreList=[];
			angular.forEach($scope.tableData,function(data){
				if(data.checked){
					batchArtificialScoreList.push(data);
				}
			});
			return batchArtificialScoreList;
		}
		//批量审批
		$scope.batchSumit = function (){
			var taskNo = "";
			var actCurrent = "N2015003";
			var batchArtificialScoreList=getBatchArtificialScoreData();
			$scope.batchArtificialScoreList=null;
			if(batchArtificialScoreList.length>0){
				$scope.batchArtificialScoreList=batchArtificialScoreList;
			}	
			if(!$scope.batchArtificialScoreList){
				 Modal.alert("请勾选审批项！");
				 return;
			}
//            }
//			IntegrityMedicalService.taskNo = taskNo;
			IntegrityMedicalService.batchTaskNos = $scope.batchArtificialScoreList;
			IntegrityMedicalService.actCurrent = actCurrent;
//			IntegrityMedicalService.doctorName = insuranceName;
//			IntegrityMedicalService.doctorCode = doctorCode;
			IntegrityMedicalService.startDate = $scope.form.date.start;
			IntegrityMedicalService.endDate = $scope.form.date.end;
			IntegrityMedicalService.saveStatus = '3';//审批批量
			var modalInstance = $modal.open({
			      templateUrl: baseUrl+'views/hcpms/achievements/integrity/integrityApproveBatchDialog_view.html',
			      backdrop:'static',
				  size:'sm',
//				  scope:scope,
				  controller: 'IntegrityApproveBatchCtrl'
			    });
			//接收返回数据
			 modalInstance.result.then(function(isTrue) {//true:审核成功
				 if (isTrue) {
					 $scope.onSubmit();
					}
	 	    });
		}
		//重置
		$scope.onReset = function (){
			//默认日期 当前日期的前一个月
			CommunityService.initDate("yyyy-MM-dd").success(function(res) {
				var endDate = res.formatDate;
				var startDate = CommunityService.lastYear(endDate);
				$scope.form.date = {
						start: startDate,
						end: endDate
				};
			});

			$scope.form.doctorCode = '';
			$scope.form.doctorName = '';
			$scope.form.orgCode = '';
			$scope.form.orgName = '';
			IntegrityMedicalService.ownerIntegritys = [];
		}
		
		var startDate = $stateParams.startDate;
		var endDate = $stateParams.endDate;
		
		if (!startDate 
				&& !endDate) {
				//默认日期 当前日期的前一个月
				CommunityService.initDate("yyyy-MM-dd").success(function(res) {
					endDate = res.formatDate;
					startDate = CommunityService.lastYear(endDate);
					$scope.form.date = {
							start: startDate,
							end: endDate
					};
					$scope.onSubmit();
				});
			}
			else {
				if (startDate 
					&& !endDate) {
					endDate = CommunityService.backwardYear(startDate);
				} 
				if (!startDate 
					&& endDate) {
					startDate = CommunityService.lastYear(endDate);
				}
				$scope.form.date = {
						start: startDate,
						end: endDate
				};
				$scope.onSubmit();
			}
		// 导出
		$scope.onExport = function() {
    		$scope.loading = true;
			var exportTotalCount = 0;//总条数
			//医务人员详情查询列表
			IntegrityMedicalService.getIntegrityAuditInfo(params()).
			success(function(res){
				//$scope.tableData = res.result;
				//$scope.totalCount = res.totals.total;
				exportTotalCount = res.totals.total;//获取总条数
			    Modal.alert("总条数:"+exportTotalCount);	
				if(exportTotalCount && exportTotalCount > 30000){
					Modal.alert("数据量超过3万条不能导出，请重新筛选！");
					return;
				}else{
					var par = params();
					var startDate = par.startDate,
					endDate = par.endDate,
					doctorCode = par.doctorCode,
					orgCode = par.orgCode,
					start = par.start,
					actCurrent = par.actCurrent,
					limit = par.limit;
					if(orgCode == null){
						orgCode = '';
					}
					if(doctorCode == null){
						doctorCode = '';
					}
					var url= baseUrl+"doctorIntegrity/medicalDetailReport.do?start="+start+"&limit="+limit+"&startDate="+startDate
					+"&endDate="+endDate+"&doctorCode="+doctorCode+"&orgCode="+orgCode+"&actCurrent="+actCurrent+"&excelName="+"医务人员审批导出";
					Modal.alert("url:"+url);
				   window.location.href = url;
				}
			});
    		$scope.loading = false;
		}
	}]).
	controller('IntegrityApproveDescCtrl',
			['$scope','IntegrityMedicalService',
			 function($scope,IntegrityMedicalService){
				var params = function(){
					var startDate = "";
					if(IntegrityMedicalService.startDate&&!angular.isUndefined(IntegrityMedicalService.startDate)){
						startDate = IntegrityMedicalService.startDate;
					}
					var endDate="";
					if(IntegrityMedicalService.endDate&&!angular.isUndefined(IntegrityMedicalService.endDate)){
						endDate = IntegrityMedicalService.endDate;
					}
					var taskNo="";
					if(IntegrityMedicalService.taskNo&&!angular.isUndefined(IntegrityMedicalService.taskNo)){
						taskNo = IntegrityMedicalService.taskNo;
					}
					var doctorCode=null;
					if(IntegrityMedicalService.doctorCode&&!angular.isUndefined(IntegrityMedicalService.doctorCode)){
						doctorCode = IntegrityMedicalService.doctorCode;
					}
					var orgCode=null;
					if(IntegrityMedicalService.orgCode&&!angular.isUndefined(IntegrityMedicalService.orgCode)){
						orgCode = IntegrityMedicalService.orgCode;
					}
					var params = {
							startDate:startDate,
							endDate:endDate,
							doctorCode:doctorCode,
							orgCode:orgCode,
							taskNo:taskNo
					}
					return params;
				};
				IntegrityMedicalService.getMedicalApproveDetail(params()).
				success(function(res){
					$scope.basefrom=res;
					$scope.insuranceName = IntegrityMedicalService.insuranceName;
					$scope.startDate = IntegrityMedicalService.startDate;
					$scope.endDate = IntegrityMedicalService.endDate;
				});
			}]);
	
})();